﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

class PauseScreen : MenuScreen
{
    SpriteFont font;

    private Texture2D backgroundTexture;
    private Vector2 backgroundPosition;

    MenuEntry returnEntry, mainMenuEntry, exitGameEntry;

    private Texture2D button1Texture, button1SelectedTexture;

    public PauseScreen()
        : base()
    {
        Globals.IsPaused = true;

        Direction = MenuEntryDirection.PAUSESCREEN;

        IsPopup = true;

        returnEntry = new MenuEntry("Return To Game");
        returnEntry.Position = new Vector2(0, 0);
        returnEntry.Selected += OnCancelPause;
        MenuEntries.Add(returnEntry);

        mainMenuEntry = new MenuEntry("Main Menu");
        mainMenuEntry.Position = new Vector2(0, 0);
        mainMenuEntry.Selected += MenuScreenSelected;
        MenuEntries.Add(mainMenuEntry);

        exitGameEntry = new MenuEntry("Exit Game");
        exitGameEntry.Position = new Vector2(0, 0);
        exitGameEntry.Selected += ExitGameSelected;
        MenuEntries.Add(exitGameEntry);

        TransitionOffTime = TimeSpan.FromSeconds(0.1);
    }


    /// <summary>
    /// Load the graphics content for this screen.
    /// </summary>
    public override void LoadContent()
    {
        // load the textures
        ContentManager content = ScreenManager.Game.Content;

        font = content.Load<SpriteFont>("SpriteFont1");

        //set font on each entry menu
        returnEntry.Font = font;
        mainMenuEntry.Font = font;
        exitGameEntry.Font = font;

        backgroundTexture = content.Load<Texture2D>(@"Textures\MenuMock\PAUSECinza copy");

        // calculate the texture positions
        Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
        backgroundPosition = new Vector2(0,0);

        // set the textures on each menu entry
        returnEntry.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\RETURN TO GAME copy");

        mainMenuEntry.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\RETURN TO MENU copy");

        exitGameEntry.SelectedTexture = content.Load<Texture2D>(@"Textures\MenuMock\QUIT GAME copy");

        base.LoadContent();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            Globals.IsPaused = false;
            ExitScreen();
            return;
        }

        base.HandleInput();
    }

    void OnCancelPause(object sender, EventArgs e)
    {
        Globals.IsPaused = false;
        OnCancel();
    }

    void MenuScreenSelected(object sender, EventArgs e)
    {
        Globals.IsPaused = false;

        ScreenManager.MainMenu();
    }

    void ExitGameSelected(object sender, EventArgs e)
    {
        Globals.IsPaused = false;

        ScreenManager.RemoveAll();
    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        // draw the background images
        spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);

        // Draw each menu entry in turn.
        for (int i = 0; i < MenuEntries.Count; i++)
        {
            MenuEntry menuEntry = MenuEntries[i];
            bool isSelected = IsActive && (i == selectedEntry);
            menuEntry.Draw(this, isSelected, gameTime);
        }

        spriteBatch.End();

        ScreenManager.Debug();
    }

}
